package pro.unal.edu.sink;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import pro.unal.edu.datasource.MyImage;
import pro.unal.edu.filter.ContrastFilter;
import pro.unal.edu.filter.GrayScaleFilter;
import pro.unal.edu.filter.InversionFilter;
import pro.unal.edu.filter.MirrorFilter;
import pro.unal.edu.filter.MyPoint;
import pro.unal.edu.filter.MyRotation;

/**
 * @author Carlos
 * @version 1.0
 * @created 17-ago-2011 03:55:43 p.m.
 */
public class MyContainer extends Canvas implements MouseListener, MouseMotionListener {

    private MyImage imgd;
    private NewJFrame frame;
    private double rotationAngle = 0;//Math.PI / 180.0d;
    private double angle = 0;
    private double contrast = 0;
    private boolean isGrayFilter = false;
    private boolean isInversionFilter = false;
    private boolean isMirrorFilter = false;
    private boolean isEdgeFilter = false;

    public MyContainer() {
    }

    public MyContainer(MyImage imgd, NewJFrame frame) {
        this.imgd = imgd;
        this.frame = frame;
        addMouseListener(this);
        addMouseMotionListener(this);
    }

    @Override
    public void paint(Graphics g) {
        BufferedImage bI = new BufferedImage(imgd.getWidth(), imgd.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics graphics = bI.getGraphics();

        InversionFilter inversionFilter = new InversionFilter();
        MirrorFilter mirrorFilter = new MirrorFilter();
        GrayScaleFilter grayScaleFilter = new GrayScaleFilter();
        ContrastFilter contrastFilter = new ContrastFilter();

        if (frame.getInvertCB().isSelected()) {
            imgd.setDataImageColor(inversionFilter.doFilter(imgd.getDataImageColor()));
        }
        if (frame.getGrayCB().isSelected()) {
            imgd.setDataImageColor(grayScaleFilter.doFilter(imgd.getDataImageColor()));
        }
        if (frame.getMirrorCB().isSelected()) {
            imgd.setDataImageColor(mirrorFilter.doFilter(imgd.getDataImageColor()));
        }
        if (frame.getBorderCB().isSelected()) {
        }
//        imgd.setDataImageColor(contrastFilter.doFilter(imgd.getDataImageColor(), contrast));

        int k = 0;
        for (int j = imgd.getHeight() - 1; j >= 0; j--) {
            for (int i = 0; i < imgd.getWidth(); i++) {
                graphics.setColor(imgd.getDataImageColor()[i][j]);
                graphics.drawLine(i, k, i, k);
            }
            k++;
        }
        g.drawImage(bI, getWidth() / 2 - imgd.getWidth() / 2, getHeight() / 2 - imgd.getHeight() / 2, null);
    }

    public MyImage getImgd() {
        return imgd;
    }

    public void setImgd(MyImage imgd) {
        this.imgd = imgd;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        click(e);
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void mouseDragged(MouseEvent e) {
    }

    @Override
    public void mouseMoved(MouseEvent e) {
    }

    public void click(MouseEvent e) {
        //Left button
        if (MouseEvent.BUTTON1 == e.getButton()) {
            angle += rotationAngle;
            MyRotation myRotation = new MyRotation(angle);
            imgd = myRotation.getImageRotated(imgd);
            repaint();
        } //Right button
        if (MouseEvent.BUTTON3 == e.getButton()) {
            angle -= rotationAngle;
            MyRotation myRotation = new MyRotation(angle);
            imgd = myRotation.getImageRotated(imgd);
            repaint();
        }
        if (MouseEvent.BUTTON2 == e.getButton()) {
            repaint();
        }
//        System.out.println((rotationAngle * 360 / 2 * Math.PI));
    }

    public double getRotationAngle() {
        return rotationAngle;
    }

    public void setRotationAngle(double rotationAngle) {
        this.rotationAngle = rotationAngle;
    }

    public double getContrast() {
        return contrast;
    }

    public void setContrast(double contrast) {
        this.contrast = contrast;
    }

    public void setIsGrayFilter(boolean isGrayFilter) {
        this.isGrayFilter = isGrayFilter;
    }

    public void setIsInversionFilter(boolean isInversionFilter) {
        this.isInversionFilter = isInversionFilter;
    }

    public void setIsMirrorFilter(boolean isMirrorFilter) {
        this.isMirrorFilter = isMirrorFilter;
    }

    public void setIsEdgeFilter(boolean isEdgeFilter) {
        this.isEdgeFilter = isEdgeFilter;
    }

    public void printMatrix(Color[][] newImage) {
        for (int i = 0; i < newImage.length; i++) {
            for (int j = 0; j < newImage[0].length; j++) {
                if (newImage[i][j] != null) {
                    System.out.print(newImage[i][j] + " ");
                }
            }
            System.out.println("");
        }
    }
}